package newcoder_exp.justPractise;
import java.io.ByteArrayInputStream;
import java.util.*;
import java.util.stream.Stream;

import org.junit.*;
public class Pra5_MiniestPath {
    public class Main {
        int m, n;
        List<int[]> res = null;
        boolean[][] flag;
        int[] px = {1, -1, 0, 0};
        int[] py = {0, 0, 1, -1};
        //寻找从起点到终点的最短路径
        public  List<int[]> findMiniumPath(int[][] nums, int x0, int y0, int x1, int y1) {
            m = nums.length;
            n = nums[0].length;
            flag = new boolean[m][n];
            dfs(nums, x0, y0, new LinkedList<>(), x1, y1);
            return res;
        }

        public void dfs(int[][] nums, int x, int y, Deque<int[]> cur,int x1, int y1) {
            if (x == x1 && y == y1) {
                if (res == null || res.size() > cur.size()) {
                    res = new ArrayList<>(cur);
                }
                return;
            }
            if (!isInArea(x, y) || flag[x][y] || nums[x][y] == 1)
                 return; //回溯
            flag[x][y] = true; 
            cur.addLast(new int[]{x, y});
            for (int i = 0; i < 4; i++) {
                dfs(nums, x + px[i], y + py[i], cur, x1, y1);
            }
            cur.removeLast();
            flag[x][y] = false;
        }

        public boolean isInArea(int x, int y) {
            return x >= 0 && x < m && y >= 0 && y < n;
        }

        public void main() {
            Scanner sc = new Scanner(System.in);
            String s = sc.nextLine();
            int[] nums = Arrays.stream(s.split(" ")).mapToInt(Integer::parseInt).toArray();
            int m = nums[0], n = nums[1];
            int[][] numbers = new int[m][n];
            for (int i = 0; i < m; i++) {
                s = sc.nextLine();
                nums = Arrays.stream(s.split(" ")).mapToInt(Integer::parseInt).toArray();
                for (int j = 0; j < n; j++) {
                    numbers[i][j] = nums[j];
                }
            }
            Main main = new Main();
            List<int[]> res = main.findMiniumPath(numbers, 0, 0, m - 1, n - 1);
            for (int i = 0; i < res.size(); i++) {
                nums = res.get(i);
                System.out.println("("+ nums[0] + "," + nums[1] +")");
            }
            sc.close();
        }
    }

    @Test
    public void test() {
        Main s = new Main();
        // int[] nums = new int[]{};
        s.main();
    }

    @Test
    public void test2() {
        System.setIn(new ByteArrayInputStream("0 1 0 0 0\n0 1 0 1 0\n0 0 0 0 0".getBytes()));
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        System.out.println(s);
        sc.close();
    }

    @Test
    public void test3() {
        String s = "0 1 0 1 0";
        // int[] num = Arrays.stream(s.split(" ")).map(str -> (int)Integer.parseInt(str)).toArray();
        // System.out.println(Arrays.toString(num));
        Stream.of(s.split(" ")).map(Integer::valueOf).forEach(e -> System.out.println(e));
    }


}
